package com.ruoyi.system.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.entity.PdfToken;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MessageUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PdfTokenMapper;
import com.ruoyi.system.service.IPdfTokenService;

/**
 * pdfTokenService业务层处理
 *
 * @author ess
 * @date 2022-05-20
 */
@Service
public class PdfTokenServiceImpl implements IPdfTokenService
{
    @Autowired
    private PdfTokenMapper pdfTokenMapper;

    /**
     * 查询pdfToken
     *
     * @param tokenId pdfToken主键
     * @return pdfToken
     */
    @Override
    public PdfToken selectPdfTokenByTokenId(String tokenId)
    {
        return pdfTokenMapper.selectPdfTokenByTokenId(tokenId);
    }

    /**
     * 查询pdfToken列表
     *
     * @param pdfToken pdfToken
     * @return pdfToken
     */
    @Override
    public List<PdfToken> selectPdfTokenList(PdfToken pdfToken)
    {
        return pdfTokenMapper.selectPdfTokenList(pdfToken);
    }

    /**
     * 新增pdfToken
     *
     * @param pdfToken pdfToken
     * @return 结果
     */
    @Override
    public int insertPdfToken(PdfToken pdfToken)
    {

        return pdfTokenMapper.insertPdfToken(pdfToken);
    }

    /**
     * 修改pdfToken
     *
     * @param pdfToken pdfToken
     * @return 结果
     */
    @Override
    public int updatePdfToken(PdfToken pdfToken)
    {
        return pdfTokenMapper.updatePdfToken(pdfToken);
    }

    /**
     * 批量删除pdfToken
     *
     * @param tokenIds 需要删除的pdfToken主键
     * @return 结果
     */
    @Override
    public int deletePdfTokenByTokenIds(String[] tokenIds)
    {
        return pdfTokenMapper.deletePdfTokenByTokenIds(tokenIds);
    }

    /**
     * 删除pdfToken信息
     *
     * @param tokenId pdfToken主键
     * @return 结果
     */
    @Override
    public int deletePdfTokenByTokenId(String tokenId)
    {
        return pdfTokenMapper.deletePdfTokenByTokenId(tokenId);
    }

    @Override
    public boolean checkToken(String tokenId) {
        PdfToken pdfToken= selectPdfTokenByTokenId(tokenId);
        if (pdfToken!=null){
            //验证状态
            int state = pdfToken.getState();
            if (state == 1){
                pdfToken.setState(0);
                updatePdfToken(pdfToken);
                long timePoor = DateUtils.getDatePoor(DateUtils.getTime(),pdfToken.getInputTime());
                return timePoor < 300000;
            }
        }
        return false;
    }

    @Override
    public String sendMessageCode(String tokenId, String phoneNumber) {
        JSONObject result = new JSONObject();
        int TemplateParam = IdUtils.RandomNumberFore();
        try {
            PdfToken pd = selectPdfTokenByTokenId(tokenId);
            pd.setVerifyCode(Integer.toString(TemplateParam));
            pd.setVerifyTime(DateUtils.getTime());
            pd.setVerifyUUID(IdUtils.simpleUUID());
            boolean sendResult =  MessageUtil.sendMessageVerifyCode("","",phoneNumber,TemplateParam);
            if (sendResult){
                updatePdfToken(pd);
                result.put("code","0000");
                result.put("msg","发送成功");
                return result.toJSONString();
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        result.put("code","0001");
        result.put("msg","发送失败");
        return result.toJSONString();
    }

    @Override
    public int addSealPassword(String tokenId, String sealPassword) {
        PdfToken pd = selectPdfTokenByTokenId(tokenId);
        pd.setVerifyCode(sealPassword);
        pd.setVerifyTime(DateUtils.getTime());
        pd.setVerifyUUID(IdUtils.simpleUUID());
        return updatePdfToken(pd);

    }
    @Override
    public String verifyMessageCode(String tokenId, String verifyCode) {
        JSONObject result = new JSONObject();
        PdfToken pd = selectPdfTokenByTokenId(tokenId);
        String code = pd.getVerifyCode();
        long timePoor = DateUtils.getDatePoor(DateUtils.getTime(),pd.getVerifyTime());
        if (timePoor < 300000){
            if (code.equals(verifyCode)){
                String uuid = IdUtils.simpleUUID();
                pd.setVerifyUUID(uuid);
                updatePdfToken(pd);
                result.put("code","0000");
                result.put("msg",uuid);
                return result.toJSONString();
            }else {
                result.put("code","0002");
                result.put("msg","验证码错误");
            }
        }else {
            result.put("code","0001");
            result.put("msg","验证码超时");
        }
        return result.toJSONString();
    }
    @Override
    public String verifyPassword(String tokenId, String verifyCode) {
        JSONObject result = new JSONObject();
        PdfToken pd = selectPdfTokenByTokenId(tokenId);
        String code = pd.getSealPassword();
        if (code.equals(verifyCode)){
            String uuid = IdUtils.simpleUUID();
            pd.setVerifyUUID(uuid);
            updatePdfToken(pd);
            result.put("code","0000");
            result.put("msg",uuid);
            return result.toJSONString();
        }else {
            result.put("code","0002");
            result.put("msg","密码错误");
        }
        return result.toJSONString();
    }

    @Override
    public boolean checkVerifyUUID(String tokenId,String uuid){
        PdfToken pd = selectPdfTokenByTokenId(tokenId);
        if(pd!=null){
            return uuid.equals(pd.getVerifyUUID());
        }
       return false;
    }
}
